import Vue from 'vue'
import Router from 'vue-router'
import store from './../store'
import CONFIG from './../config'
Vue.use(Router)

const router = new Router({
  mode:'history',
  routes: [
    {
      path:'/',
      redirect:'/homepage'
    },
    {
      path: '/',
      component: resolve => require(['../components/common/Home.vue'], resolve),
      children:[
        {
          path: '/verified',
          component: resolve => require(['../components/page/Verified.vue'], resolve),
          meta: { title: '实名认证' }
        },
        {
          path: '/moneyCenter',
          component: resolve => require(['../components/page/moneyCenter.vue'], resolve),
          meta: { title: '财务中心' }
        },
        {
          path: '/perfectpersonal',
          component: resolve => require(['../components/page/PerfectPersonal.vue'], resolve),
          meta: { title: '填写个人信息' }
        },
        {
          path: '/loanstaging',
          component: resolve => require(['../components/page/LoanStaging.vue'], resolve),
          meta: { title: '填写贷款信息' }
        },
        {
          path: '/carupload',
          component: resolve => require(['../components/page/CarUpload.vue'], resolve),
          meta: { title: '上传资料' }
        },
        {
          path: '/Setting',
          component: resolve => require(['../components/page/Setting.vue'], resolve),
          meta: { title: '系统设置' }
        },
        {
          path: '/changeInfo',
          component: resolve => require(['../components/page/ChangeInfo.vue'], resolve),
          meta: { title: '修改系统设置' }
        },
        {
          path: '/premiumstaging',
          component: resolve => require(['../components/page/PremiumStaging.vue'], resolve),
          meta: { title: '保费分期申请表' }
        },

        {
          path: '/LoanAnnal',
          component: resolve => require(['../components/page/LoanAnnal.vue'], resolve),
          meta: { title: '贷款记录' }
        },
        {
          path: '/LoanDetails',
          component: resolve => require(['../components/page/LoanDetails.vue'], resolve),
          meta: { title: '贷款详情' }
        },
        {
          path: '/plan',
          component: resolve => require(['../components/page/Plan.vue'], resolve),
          meta: { title: '还款计划' }
        },
        {
          path: '/WaitPay',
          component: resolve => require(['../components/page/WaitPay.vue'], resolve),
          meta: { title: '待支付' }
        },

        {
          path: '/LnterveningPact',
          component: resolve => require(['../components/protocol/LnterveningPact.vue'], resolve),
          meta: { title: '居间协议' }
        },
        {
          path: '/LoginPact',
          component: resolve => require(['../components/protocol/LoginPact.vue'], resolve),
          meta: { title: '注册协议' }
        },
        {
          path: '/SecrecyPact',
          component: resolve => require(['../components/protocol/SecrecyPact.vue'], resolve),
          meta: { title: '隐私权政策' }
        },
        {
          path: '/ServingPact',
          component: resolve => require(['../components/protocol/ServingPact.vue'], resolve),
          meta: { title: '业务确认书' }
        },
        {
          path: '/uploadpact',
          component: resolve => require(['../components/protocol/UploadPact.vue'], resolve),
          meta: { title: '上传资料的协议集中' }
        },
        {
          path: '/dashboard',
          component: resolve => require(['../components/page/DashBoard.vue'], resolve),
          meta: { title: '汽车金融' }
        },
        {
          path: '/aboutus',
          component: resolve => require(['../components/page/AboutUs.vue'], resolve),
          meta: { title: '关于我们' }
        },
        {
          path: '/newslist',
          component: resolve => require(['../components/page/NewsList.vue'], resolve),
          meta: { title: '新闻资讯' }
        },
        {
          path: '/newsdetail',
          component: resolve => require(['../components/page/NewsDetail.vue'], resolve),
          meta: { title: '新闻详情' }
        },
        {
          path: '/recruitment',
          component: resolve => require(['../components/page/Recruitment.vue'], resolve),
          meta: { title: '招聘' }
        },
        {
          path: '/supplyChain',
          component: resolve => require(['../components/page/SupplyChain.vue'], resolve),
          meta: { title: '供应链金融' }
        },
      ]
    },
    {
      path: '/',
      component: resolve => require(['../components/common/Home2.vue'], resolve),
      children:[
        {
          path: '/homepage',
          component: resolve => require(['../components/page/HomePage.vue'], resolve),
          meta: { title: '首页' }
        },
        {
          path: '/personal',
          component: resolve => require(['../components/page/Personal.vue'], resolve),
          meta: { title: '个人中心' }
        },
      ]
    },
    {
      path: '/DelayList',
      component: resolve => require(['../components/page/DelayList.vue'], resolve),
      meta: { title: '逾期记录' }
    },
    {
      path: '/login',
      component: resolve => require(['../components/page/Login.vue'], resolve),
      meta: { title: '登录' }
    },
    {
      path: '/register',
      component: resolve => require(['../components/page/Register.vue'], resolve),
      meta: { title: '注册' }
    },
  ]
})
router.beforeEach((to,form,next)=>{
  //只有部分需要拦截
  let title;
  if (!CONFIG.token &&  to.path !== '/login' ) {
    if(to.path === '/homepage' || to.path === '/register' || to.path === '/aboutus' || to.path === '/dashboard' || to.path === '/newslist' || to.path === '/newsdetail' || to.path === '/recruitment' || to.path === '/supplyChain'){
      title = to.meta.title;
      next();
    }else {
      title = '登陆';
      next('/login');
    }
  } else {
    title = to.meta.title;
    next()
  }
  document.title =title;
  store.dispatch('changeTitleAction',title);
});

export default router;
